home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / GAMES / C014.ZIP / LARN_SRC.ZIP / SIGNAL.C < prev    next >
C/C++ Source or Header  |  1993-11-17  |  5KB  |  176 lines

  1. #include <signal.h>
  2. #include "header.h"
  3. #include "larndefs.h"
  4.  
  5. #define BIT(a) (1<<((a)-1))
  6.  
  7. extern char savefilename[],wizard,predostuff,nosignal;
  8.  
  9. static s2choose()   /* text to be displayed if ^C during intro screen */
  10.     {
  11.     cursor(1,24); lprcat("Press "); setbold(); lprcat("return"); resetbold();
  12.     lprcat(" to continue: ");   lflush();
  13.     }
  14.  
  15. static void cntlc()    /* what to do for a ^C */
  16.     {
  17.     if (nosignal) return;   /* don't do anything if inhibited */
  18. # ifndef MSDOS
  19.     signal(SIGQUIT,SIG_IGN);
  20. # endif
  21.     signal(SIGINT,SIG_IGN);
  22.     quit(); if (predostuff==1) s2choose(); else showplayer();
  23.     lflush();
  24. # ifndef MSDOS
  25.     signal(SIGQUIT,cntlc);
  26. # endif
  27.     signal(SIGINT,cntlc);
  28.     }
  29.  
  30. #ifndef MSDOS
  31. /*
  32.  *  subroutine to save the game if a hangup signal
  33.  */
  34. static sgam()
  35.     {
  36.     savegame(savefilename);  wizard=1;  died(-257); /* hangup signal */
  37.     }
  38. #endif
  39.  
  40. #ifdef SIGTSTP
  41. static tstop() /* control Y */
  42.     {
  43.     if (nosignal)   return;  /* nothing if inhibited */
  44.     lcreat((char*)0);  clearvt100();    lflush();     signal(SIGTSTP,SIG_DFL);
  45. #ifdef SIGVTALRM
  46.     /* looks like BSD4.2 or higher - must clr mask for signal to take effect*/
  47.     sigsetmask(sigblock(0)& ~BIT(SIGTSTP));
  48. #endif
  49.     kill(getpid(),SIGTSTP);
  50.  
  51.     setupvt100();  signal(SIGTSTP,tstop);
  52.     if (predostuff==1) s2choose(); else drawscreen();
  53.     showplayer();   lflush();
  54.     }
  55. #endif SIGTSTP
  56.  
  57. /*
  58.  *  subroutine to issue the needed signal traps  called from main()
  59.  */
  60. static void sigfpe()  { sigpanic(SIGFPE); }
  61. # ifndef MSDOS
  62. static sigbus()  { sigpanic(SIGBUS); }
  63. static sigill()  { sigpanic(SIGILL); }   static sigtrap() { sigpanic(SIGTRAP); }
  64. static sigiot()  { sigpanic(SIGIOT); }   static sigemt()  { sigpanic(SIGEMT); }
  65. static sigsegv() { sigpanic(SIGSEGV); }  static sigsys()  { sigpanic(SIGSYS); }
  66. static sigpipe() { sigpanic(SIGPIPE); }  static sigterm() { sigpanic(SIGTERM); }
  67. # endif
  68.  
  69. sigsetup()
  70.     {
  71.     signal(SIGINT,  cntlc);
  72.     signal(SIGFPE,  sigfpe);
  73. # ifndef MSDOS
  74.     signal(SIGBUS,  sigbus);        signal(SIGQUIT, cntlc);
  75.     signal(SIGKILL, SIG_IGN);       signal(SIGHUP,  sgam);
  76.     signal(SIGILL,  sigill);        signal(SIGTRAP, sigtrap);
  77.     signal(SIGIOT,  sigiot);        signal(SIGEMT,  sigemt);
  78.     signal(SIGSEGV, sigsegv);       signal(SIGSYS,  sigsys);
  79.     signal(SIGPIPE, sigpipe);       signal(SIGTERM, sigterm);
  80. #ifdef SIGTSTP
  81.     signal(SIGTSTP,tstop);      signal(SIGSTOP,tstop);
  82. #endif SIGTSTP
  83. # endif
  84.     }
  85.  
  86. #ifdef MSDOS
  87. #define NSIG 9
  88. #endif
  89.  
  90. #ifdef VMS
  91. #define NSIG 16
  92. #endif
  93.  
  94. #ifdef BSD  /* for BSD UNIX? */
  95.  
  96. static char *signame[NSIG] = { "",
  97. "SIGHUP",  /*   1    hangup */
  98. "SIGINT",  /*   2    interrupt */
  99. "SIGQUIT", /*   3    quit */
  100. "SIGILL",  /*   4    illegal instruction (not reset when caught) */
  101. "SIGTRAP", /*   5    trace trap (not reset when caught) */
  102. "SIGIOT",  /*   6    IOT instruction */
  103. "SIGEMT",  /*   7    EMT instruction */
  104. "SIGFPE",  /*   8    floating point exception */
  105. "SIGKILL", /*   9    kill (cannot be caught or ignored) */
  106. "SIGBUS",  /*   10   bus error */
  107. "SIGSEGV", /*   11   segmentation violation */
  108. "SIGSYS",  /*   12   bad argument to system call */
  109. "SIGPIPE", /*   13   write on a pipe with no one to read it */
  110. "SIGALRM", /*   14   alarm clock */
  111. "SIGTERM", /*   15   software termination signal from kill */
  112. "SIGURG",  /*   16   urgent condition on IO channel */
  113. "SIGSTOP", /*   17   sendable stop signal not from tty */
  114. "SIGTSTP", /*   18   stop signal from tty */
  115. "SIGCONT", /*   19   continue a stopped process */
  116. "SIGCHLD", /*   20   to parent on child stop or exit */
  117. "SIGTTIN", /*   21   to readers pgrp upon background tty read */
  118. "SIGTTOU", /*   22   like TTIN for output if (tp->t_local<OSTOP) */
  119. "SIGIO",   /*   23   input/output possible signal */
  120. "SIGXCPU", /*   24   exceeded CPU time limit */
  121. "SIGXFSZ", /*   25   exceeded file size limit */
  122. "SIGVTALRM",/*  26   virtual time alarm */
  123. "SIGPROF", /*   27   profiling time alarm */
  124. "","","","" };
  125.  
  126. #else BSD   /* for system V? */
  127.  
  128. static char *signame[NSIG+1] = { "",
  129. "SIGHUP",  /*   1    hangup */
  130. "SIGINT",  /*   2    interrupt */
  131. "SIGQUIT", /*   3    quit */
  132. "SIGILL",  /*   4    illegal instruction (not reset when caught) */
  133. "SIGTRAP", /*   5    trace trap (not reset when caught) */
  134. "SIGIOT",  /*   6    IOT instruction */
  135. "SIGEMT",  /*   7    EMT instruction */
  136. # ifdef MSDOS
  137. "SIGFPE"}; /*   8    floating point exception */
  138. # else MSDOS
  139. "SIGFPE",  /*   8    floating point exception */
  140. "SIGKILL", /*   9    kill (cannot be caught or ignored) */
  141. "SIGBUS",  /*   10   bus error */
  142. "SIGSEGV", /*   11   segmentation violation */
  143. "SIGSYS",  /*   12   bad argument to system call */
  144. "SIGPIPE", /*   13   write on a pipe with no one to read it */
  145. "SIGALRM", /*   14   alarm clock */
  146. # ifdef VMS
  147. "SIGTERM"}; /*  15   software termination signal from kill */
  148. # else VMS
  149. "SIGTERM", /*   15   software termination signal from kill */
  150. "SIGUSR1",  /*  16   user defines signal 1 */
  151. "SIGUSR2", /*   17   user defines signal 2 */
  152. "SIGCLD",  /*   18   child death */
  153. "SIGPWR" };  /*   19   power fail */
  154. # endif VMS
  155. # endif MSDOS
  156. # endif BSD
  157.  
  158. /*
  159.  *  routine to process a fatal error signal
  160.  */
  161. static sigpanic(sig)
  162. int sig;
  163. {
  164.     char buf[128];
  165.     signal(sig,SIG_DFL);
  166.     sprintf(buf,"\nLarn - Panic! Signal %d received [%s]",sig,signame[sig]);
  167.     write(2,buf,strlen(buf));  sleep(2);
  168.     sncbr();
  169.     savegame(savefilename); 
  170. # ifdef MSDOS
  171.     exit(1);
  172. # else
  173.     kill(getpid(),sig); /* this will terminate us */
  174. # endif
  175. }
  176.